package com.ricohgag.controller.admin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.GoodsCatalog;
import com.ricohgag.service.GoodsCatalogService;
import com.ricohgag.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/admin/goodsCatalog")
public class GoodsCatalogController {
    @Autowired
    private GoodsCatalogService goodsCatalogService;

    @RequestMapping("/index")
    public ModelAndView toIndex(String name, @RequestParam(value = "pageNow", required = false)  Integer current, HttpServletRequest request) throws Exception{
        ModelAndView mav = new ModelAndView("/admin/goodsCatalog/index");
        Pager<GoodsCatalog> pager = new Pager<>();

        String url = request.getContextPath()+"/admin/goodsCatalog/index";
        pager.setUrl(url);
        pager.setPageNum(10);
        if(current == null){
            current = 1;
        }
        Page<GoodsCatalog> page = new Page<>(current, 10);

        IPage<GoodsCatalog> goodsIPage = goodsCatalogService.selectCatalogPage(name, page);

        System.err.println("name: "+name);
        mav.addObject("name",name);
        mav.addObject("Pager", goodsIPage);
        mav.addObject("pages", goodsIPage.getPages());
        mav.addObject("url",url);
        return mav;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(Integer id) throws Exception{
        ModelAndView mav = new ModelAndView("admin/goodsCatalog/saveOrUpdate");
        if(!"0".equals(id)){
            GoodsCatalog goodsCatalog = goodsCatalogService.selectCatalogOne(id);
            mav.addObject("goodsCatalog", goodsCatalog);
        }

        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(GoodsCatalog goodsCatalog, MultipartFile imgFile, HttpServletRequest request) throws Exception {
        if(goodsCatalog.getId() == null){
            goodsCatalogService.insertCatalog(goodsCatalog);
        } else {
            goodsCatalogService.updateCatalog(goodsCatalog);
        }

        return "/common/public/reload";
    }

    @RequestMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteShopAd(Integer id, HttpServletResponse response) throws Exception{
        Map<String, String> map = new HashMap<>();

        int count = goodsCatalogService.deleteCatalog(id);
        System.out.println("delete: "+count);
        if(count > 0){
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }

        String jsonStr = JSON.toJSONString(map);
        return jsonStr;
    }

}

